<?php
/**
 * MVC-путь primary-узла
 *
 * @since 06.11.2011
 * @author Akulov Denis <akulov.d.g@gmail.com>
 */
class K_MVC_Path_Primary extends K_MVC_Path_Abstract {

    public function __construct(K_MVC_PathCorrector $objCorrector, $request) {
        $request = $objCorrector->requestCorrect($request);
        $arRequest = explode('/', $request);
        $this->controller = array_shift($arRequest);

        if ($this->controller == 'ajax') {
            $this->controller = array_shift($arRequest);
            $this->isAjax = true;
        }

        if (!preg_match(ROUTING_FILTER_PATH, $this->controller)) {
            throw new Exception('Not valid contoller: '.$this->controller);
        }

        if (substr($this->controller, 0, 1) == '_') {
            throw new Exception('Controller name is reserved: '.$this->controller);
        }

        $this->method = array_shift($arRequest);
        if (!preg_match(ROUTING_FILTER_PATH, $this->method)) {
            throw new Exception('Not valid method: '.$this->method);
        }

        if (substr($this->method, 0, 1) == '_') {
            throw new Exception('Method name is reserved: '.$this->method);
        }

        if (substr($this->method, -6, 6) == 'submit') {
            throw new Exception('Method name is reserved: '.$this->method);
        }

        $this->params = (array_values($arRequest));
        foreach ($this->params as $item) {
            if (!preg_match(ROUTING_FILTER_PARAM, $item)) {
                throw new Exception('Not valid params: '.print_r($this->params, true));
            }
        }

        $this->ToLowerCamelCase();
    }

}